﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class MaterialAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //设置经办人
              Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.BindManufacturerID();
                this.BindManufacturerCode();
                this.BindMaterialCategoryID();
            }
        }
        /// </summary>
        private void BindManufacturerID()
        {
          //  UART.BLL.Sales.Manufacturer bll = new UART.BLL.Sales.Manufacturer();
 var ds = mk.l.Sales_Manufacturer.ToList();
            this.ddlManufacturerID.DataSource = ds;
            this.ddlManufacturerID.DataTextField = "ManufacturerName";
            this.ddlManufacturerID.DataValueField = "ManufacturerID";
            this.ddlManufacturerID.DataBind();
            //ListItem li = new ListItem("不限", "0");
            //this.ddlManufacturerID.Items.Add(li);
            //this.ddlManufacturerID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// Binds the manufacturer code.
        /// </summary>
        private void BindManufacturerCode()
        {
         //   UART.BLL.Sales.ManufacturerCode bll = new UART.BLL.Sales.ManufacturerCode();
       var ds = mk.l.Sales_ManufacturerCode.Where(y=>y.ManufacturerID.ToString()==this.ddlManufacturerID.SelectedValue).ToList();
            this.rbtnlCode.DataSource = ds;
            this.rbtnlCode.DataTextField = "Code";
            this.rbtnlCode.DataValueField = "Code";
            this.rbtnlCode.DataBind();
        }

        /// <summary>
        /// Binds the material category ID.绑定材料类型单选列表
        /// </summary>
        private void BindMaterialCategoryID()
        {
          //  UART.BLL.Sales.MaterialCategory bll = new UART.BLL.Sales.MaterialCategory();
         var ds = mk.l.Sales_MaterialCategory.ToList();
            this.rbtnlMaterialCategoryID.DataSource = ds;
            this.rbtnlMaterialCategoryID.DataTextField = "Name";
            this.rbtnlMaterialCategoryID.DataValueField = "MaterialCategoryID";
            this.rbtnlMaterialCategoryID.DataBind();
        }

        /// <summary>
        /// Handles the ServerValidate event of the CustomValidator1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            var bll = mk.l.Sales_Material.ToList();
            if (bll.Where(y=>y.MaterialCategoryID==int.Parse(this.rbtnlMaterialCategoryID.SelectedValue)&y.Name== mk.InputText(this.txtName.Text, 50)).Count()>0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
             //   UART.BLL.Sales.Material bll = new UART.BLL.Sales.Material();
                Sales_Material m = new Sales_Material();
                m.ManufacturerID = int.Parse(this.ddlManufacturerID.SelectedValue);
                m.MaterialCategoryID = int.Parse(this.rbtnlMaterialCategoryID.SelectedValue);
                m.Name = mk.InputText(this.txtName.Text, 50);
                m.ManufacturerCode = rbtnlCode.SelectedValue;
                m.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
                try
                {
                 ///   bll.Add(m);
                 ///   
                    mk.l.Sales_Material.InsertOnSubmit(m);
                    mk.l.SubmitChanges();
                    mk.Alert("添加成功！", "MaterialList.aspx");
                }
                catch (Exception ex)
                {
                    mk.Alert("添加失败！错误信息：" + ex.Message, "MaterialAdd.aspx");
                }

            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlManufacturerID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlManufacturerID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindManufacturerCode();
        }
    }
}
